/**
 * Created by xiaolinzhang on 2016/11/19.
 */

var acc = require('../libs/acc');
var logger = require('../libs/log').logger;
var async = require('async');

exports.sessionGet = function (req,callback) {
    logger.debug("data"+JSON.stringify(req.session));
    async.waterfall([
        function(cb){
            acc.redisGet(req.session.id,cb);
        },
        function (result,cb) {
            logger.debug(result);
            cb(null,result);
        }
    ],function (err,result) {
        if(err){
            logger.error(err);
            callback(err,null);
        }else {
            callback(null,result);
        }
    });

}


exports.sessionSet = function (req,callback) {
    logger.debug("data"+JSON.stringify(req.session.id));
    async.series({
        getr:function(cb){
            var data = {
                name:req.session.acc_name,
                channel_name:req.session.channel_name,
                org_id:req.session.org_id,
                channel_id:req.session.channel_id,
                emp_id:req.session.emp_id,
                realname:req.session.realname,
                pay_method:req.session.acctype,
                openid:req.session.openid,
                wxnickname:req.session.wxnickname,
                avatar:req.session.avatar
            };
            acc.redisSave(req.session.id,JSON.stringify(data),cb);
        }
    },function (err,result) {
        if(err){
            logger.error(err);
            callback(err,null);
        }else {
            callback(null,{code:0});
        }
    });

}